package databasetest.zx.com.databasetest.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import databasetest.zx.com.databasetest.model.UserModel;

/**
 * Created by zhoux on 2017/12/11.
 * 注释：
 */

public class MyDataBaseHelper extends SQLiteOpenHelper {
    public String TAG = "MyDataBaseHelper";

    public MyDataBaseHelper(Context mContext, String databaseName, SQLiteDatabase.CursorFactory factory, int version) {
        super(mContext, databaseName, factory, version);
    }

    public static final String USER_TABLE_NAME = "USER_TABLE";//表名
    private static final String CREATE_USER_TABLE = new StringBuffer(//创建表的SQL
            "CREATE TABLE IF NOT EXISTS "
                    + USER_TABLE_NAME
                    + String.format(
                    "("
                            + "%s INTEGER PRIMARY KEY AUTOINCREMENT, " // id, download id
                            + "%s VARCHAR, " // name
                            + "%s INTEGER " //  age
                            + ")"
                    , UserModel.Columns._ID
                    , UserModel.Columns.NAME
                    , UserModel.Columns.AGE
            )
    )
            .toString();


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_USER_TABLE);//创建表
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //根据版本号更新表
        switch (oldVersion) {
            case 1://todo 增加表、删除表、增加字段、删除字段、等操作
            case 2:


        }
    }


    /**
     * 给一张表增加一个字段
     *
     * @param db
     * @param dbTable
     * @param columnName
     * @param columnDefinition
     */
    private void addColumn(SQLiteDatabase db, String dbTable,
                           String columnName, String columnDefinition) {

        if (checkColumnExists(db, dbTable, columnName)) {
            Log.e("checkColumnExists", "true");
        } else {
            Log.e("checkColumnExists", "false");
            db.execSQL("ALTER TABLE " + dbTable + " ADD COLUMN " + columnName + " "
                    + columnDefinition);
        }
    }

    /**
     * 检查表中某列是否存在
     *
     * @param db
     * @param tableName  表名
     * @param columnName 列名
     * @return
     */
    private boolean checkColumnExists(SQLiteDatabase db, String tableName, String columnName) {
        boolean result = false;
        Cursor cursor = null;

        try {
            cursor = db.rawQuery("select * from sqlite_master where name =? and sql like ?"
                    , new String[]{tableName, "%" + columnName + "%"});
            result = null != cursor && cursor.moveToFirst();
        } catch (Exception e) {
            Log.e(TAG, "checkColumnExists2..." + e.getMessage());
        } finally {
            if (null != cursor && !cursor.isClosed()) {
                cursor.close();
            }
        }

        return result;
    }
}
